﻿using System;
using System.Windows.Forms;
using SasPlugIn.SASCli;

namespace SasPlugIn
{
    /// <summary>
    /// Dialog box used to define properties used for accessing SAS as client
    /// </summary>
    public partial class DlgSasClientConfig : Form
    {
        public IomProperties Properties { get; set; }

        #region Constructor

        public DlgSasClientConfig(IomProperties p)
        {
            InitializeComponent();
            Properties = p;

            BindToControls(Properties);
        }
        #endregion

        #region Binding

        private void BindToControls(IomProperties p)
        {
            textBoxDnsName.Text = p.DnsName;
            textBoxServerDefName.Text = p.ServerDefName;
            textBoxWorkspaceName.Text = p.WorkspaceName;
            numTextBoxPort.Text = p.ServerPort.ToString();
            textBoxTablePrefix.Text = p.TablePrefix;
            textBoxLibPath.Text = p.LibPath;
            textBoxUserName.Text = p.LoginName;
            textBoxPwd.Text = p.CryptedPwd;
            textBoxPwdBis.Text = p.CryptedPwd;
        }

        private void BindToProperties()
        {
            Properties.DnsName = textBoxDnsName.Text;
            Properties.ServerDefName = textBoxServerDefName.Text;
            Properties.WorkspaceName = textBoxWorkspaceName.Text;
            Properties.ServerPort = short.Parse(numTextBoxPort.Text);
            Properties.TablePrefix = textBoxTablePrefix.Text;
            Properties.LibPath  = textBoxLibPath.Text;

            Properties.LoginName = textBoxUserName.Text;
            Properties.CryptedPwd = textBoxPwd.Text;
        }

        #endregion

        #region Buttons Event

        private void buttonLoadMap_Click(object sender, EventArgs e)
        {
            try
            {
                openDialog.Filter = "SAS Client config files|*.cfg|All Files|*.*";
                if (openDialog.ShowDialog() != DialogResult.OK) return;
                {
                    Properties.XmlRead(openDialog.FileName);
                    BindToControls(Properties);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load config file: \n" + ex.Message);
            }


        }

        private void buttonSaveMap_Click(object sender, EventArgs e)
        {
            try
            {
                BindToProperties();

                saveFileDialog.FileName = "SasClient.cfg";
                saveFileDialog.Filter = "SAS Client config files|*.cfg|All Files|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    Properties.XmlWrite(saveFileDialog.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save config file: \n" + ex.Message);
            }


        }

        private void buttonResetCfg_Click(object sender, EventArgs e)
        {
            Properties.Init();
            BindToControls(Properties);

        }
        #endregion

        #region Closing

        private void DlgSasClientConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            BindToProperties();
        }
        #endregion

    }
}
